/**
 * 
 */
package ro.dta.idbi.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JLabel;

/**
 * Label with a gradient background for better UI
 * 
 * @author Tudor Dobrila
 */
public class JTitleLabel extends JLabel {

	private static final long serialVersionUID = -1127965249773028034L;

	private Color baseColor = new Color(250, 250, 250);

	/**
	 * JTitleLabel class constructor
	 * 
	 * @param text
	 *            Text to display in the label
	 */
	public JTitleLabel(final String text) {
		super(text);
		setOpaque(true);
	}

	/**
	 * JTitleLabel class constructor
	 * 
	 * @param text
	 *            Test to display in the label
	 * @param baseColor
	 *            Base color for the background gradient
	 */
	public JTitleLabel(final String text, final Color baseColor) {
		this(text);
		this.baseColor = baseColor;
	}

	@Override
	protected void paintComponent(final Graphics graphics) {
		if (!isOpaque()) {
			super.paintComponent(graphics);
			return;
		}

		final Graphics2D g2d = (Graphics2D) graphics;
		final int width = getWidth();
		final int height = getHeight();
		final Color color1 = baseColor;
		final Color color2 = color1.darker();
		final GradientPaint gradient = new GradientPaint(0, 0, color1, 0, height, color2);

		g2d.setPaint(gradient);
		g2d.fillRect(0, 0, width, height);

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setColor(getForeground());
		g2d.setFont(g2d.getFont().deriveFont(Font.BOLD));
		g2d.drawString(super.getText(), 7, height / 2 + 5);
	}

	/**
	 * @param baseColor
	 *            the baseColor to set
	 */
	public void setBaseColor(final Color baseColor) {
		this.baseColor = baseColor;
	}

	/**
	 * @return the baseColor
	 */
	public Color getBaseColor() {
		return baseColor;
	}
}
